﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UtilZ.Dotnet.Ex.DataDictionaryValueDescription
{
    /// <summary>
    /// 数据字典管理器类
    /// </summary>
    public class DataDictionaryManager
    {
        private readonly static Dictionary<object, TypeDataDictionary> _typeDataDictionaryDic = new Dictionary<object, TypeDataDictionary>();

        /// <summary>
        /// 注册数据字典
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        public static void Registe<T>()
        {
            Type type = typeof(T);
            var dataDictionary = new TypeDataDictionary(type);
            Registe(type, dataDictionary);
        }

        /// <summary>
        /// 注册数据字典
        /// </summary>
        /// <param name="groupId">数据字典标识</param>
        /// <param name="dataDictionary">数据字典</param>
        public static void Registe(object groupId, TypeDataDictionary dataDictionary)
        {
            if (groupId == null)
            {
                throw new ArgumentNullException(nameof(groupId));
            }

            if (dataDictionary == null)
            {
                throw new ArgumentNullException(nameof(dataDictionary));
            }

            _typeDataDictionaryDic.Add(groupId, dataDictionary);
        }



        /// <summary>
        /// 根据类型获取数据字典,不存在返回null
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <returns>数据字典</returns>
        public static TypeDataDictionary GetDataDictionary<T>()
        {
            return GetDataDictionaryById(typeof(T));
        }

        /// <summary>
        /// 根据数据字典标识获取数据字典,不存在返回null
        /// </summary>
        /// <param name="groupId">数据字典标识</param>
        /// <returns>数据字典</returns>
        public static TypeDataDictionary GetDataDictionaryById(object groupId)
        {
            if (groupId == null)
            {
                throw new ArgumentNullException(nameof(groupId));
            }

            TypeDataDictionary group;
            _typeDataDictionaryDic.TryGetValue(groupId, out group);
            return group;
        }






        /// <summary>
        /// 根据数据字典类型移除数据字典,[移除成功返回true,否则返回false]
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <returns>[移除成功返回true,否则返回false]</returns>
        public static bool Remove<T>()
        {
            return RemoveById(typeof(T));
        }

        /// <summary>
        /// 根据数据字典标识移除数据字典,[移除成功返回true,否则返回false]
        /// </summary>
        /// <param name="groupId">数据字典标识</param>
        /// <returns>[移除成功返回true,否则返回false]</returns>
        public static bool RemoveById(object groupId)
        {
            return _typeDataDictionaryDic.Remove(groupId);
        }





        /// <summary>
        /// 清空值所有数据字典
        /// </summary>
        public static void Clear()
        {
            _typeDataDictionaryDic.Clear();
        }






        /// <summary>
        /// 清空指定数据字典类型的数据字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void Clear<T>()
        {
            Clear(typeof(T));
        }

        /// <summary>
        /// 清空指定数据字典标识的数据字典
        /// </summary>
        /// <param name="groupId">组标识</param>
        public static void Clear(object groupId)
        {
            TypeDataDictionary dataDictionary = GetDataDictionaryById(groupId);
            if (dataDictionary != null)
            {
                dataDictionary.Clear();
            }
        }






    }
}
